// ==UserScript==
// @name         YBFetchLast
// @namespace    https://bbs.tampermonkey.net.cn/
// @version      0.1.0
// @description  try to take over the world!
// @author       You
// @grant GM_xmlhttpRequest
// @match        https://wm.xuanqingkj.drop.cn/jrNJyBfHxQ.php/dashboard*
// ==/UserScript==

(function() {
    'use strict';
    let urls_last='http://127.0.0.1/index.php/fetcher/getLastDateUrlDict'+'?t='+new Date().getTime();
    let url_create_table='http://127.0.0.1/index.php/fetcher/createtable'
    let req={
        url:urls_last,
        method :"GET",
        onload:function(xhr){
            let urlJSON=JSON.parse(xhr.responseText)
            if(urlJSON){
                for (let key in urlJSON){
                    fetchData(urlJSON[key],key)
                }
            }
        }
    }
    GM_xmlhttpRequest(req);

    function fetchData(url,key){
        fetch(url, {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
                'X-Requested-With': 'XMLHttpRequest',
            },
        })
            .then(response => {
                if(!response.ok){
                    console.log('!response.ok'+url)
                    return false
                }
                return response.json();
            })
            .then(data => {
                    console.log(45,data.rows)
                    let req={
                        url:url_create_table+'?table='+key+'&total='+data.total,
                        method :"POST",
                        data:'data='+encodeURIComponent(JSON.stringify(data.rows)),
                        headers: {
                            "Content-type": "application/x-www-form-urlencoded"
                        },
                        onload:function(xhr){
                            console.log(54,xhr.responseText)
                        }
                    }
                    GM_xmlhttpRequest(req);
                }
            )
    }
    // Your code here...
})();